package pub.guoxin.oops.auth.service.impl;

import com.google.common.collect.Lists;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.auth.service.UserService;
import pub.guoxin.oops.auth.vo.RoleUserVo;
import pub.guoxin.oops.auth.vo.UserInfoChangeVo;
import pub.guoxin.oops.auth.vo.UserVo;
import pub.guoxin.oops.common.auth.utils.BCryptPasswordEncoderUtils;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.model.QUser;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.repository.UserRepository;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ResultVoBuilder builder;

    @Override
    public Page<UserVo> page(User user, Authority authority, Integer pageNumber, Integer pageSize) {
        QUser qUser = QUser.user;
        BooleanExpression expression = qUser.used.isTrue();
        expression = expression.and(qUser.companyId.eq(user.getCompanyId()));
        if (!Objects.isNull(authority)) {
            expression = expression.and(qUser.authorities.contains(authority));
        }

        Pageable request = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "creation");
        Page<User> page = userRepository.findAll(expression, request);
        return page.map(UserVo::new);
    }

    @Override
    public ResultVo delete(User user, String username) {
        User one = userRepository.findByMobile(username);
        if (Objects.isNull(one)) {
            return builder.buildBadRequestResultInfo("该用户不存在");
        }

        Authority authorityEnum = one.findFirstAuthorityEnum();
        if (!user.findFirstAuthorityEnum().isAuthorityGreatThanOrEqualTo(authorityEnum)) {
            return builder.buildBadRequestResultInfo("您的权限不可以进行此操作");
        }
        one.setUsed(false);
        userRepository.save(one);
        return builder.buildSuccessResultInfo();
    }

    @Override
    public ResultVo update(User user, UserInfoChangeVo userChangeVo) {
        User one = userRepository.findByMobile(userChangeVo.getUsername());
        if (Objects.isNull(one)) {
            return builder.buildBadRequestResultInfo("该用户不存在");
        }

        Authority authorityEnum = one.findFirstAuthorityEnum();
        if (!user.findFirstAuthorityEnum().isAuthorityGreatThanOrEqualTo(authorityEnum)) {
            return builder.buildBadRequestResultInfo("您的权限不可以进行此操作");
        }
        one.setFirstname(userChangeVo.getName());
        userRepository.save(one);
        return builder.buildSuccessResultInfo("修改成功");
    }

    @Override
    public ResultVo<UserVo> one(User user, String username) {
        User one = userRepository.findByMobile(username);
        if (Objects.isNull(one)) {
            return builder.buildBadRequestResultInfo("该用户不存在");
        }

        Authority authorityEnum = one.findFirstAuthorityEnum();
        if (!user.findFirstAuthorityEnum().isAuthorityGreatThanOrEqualTo(authorityEnum)) {
            return builder.buildBadRequestResultInfo("您的权限不可以进行此操作");
        }
        return builder.buildSuccessResultInfo(new UserVo(one));
    }

    @Override
    public User one(String username) {
        return userRepository.findByMobile(username);
    }

    @Override
    public List<RoleUserVo> getRoleUser(User user) {
        List<User> users = userRepository.findByCompanyIdAndAuthoritiesIsContaining(user.getCompanyId(), Authority.ROLE_USER);
        return users.stream().map(RoleUserVo::new).collect(Collectors.toList());
    }

    @Override
    public ResultVo create(User user, UserInfoChangeVo userChangeVo) {
        User one = userRepository.findByMobile(userChangeVo.getUsername());
        if (!Objects.isNull(one)) {
            return builder.buildBadRequestResultInfo("该子帐户管理员以存在");
        }
        User insertUser = new User();
        insertUser.setFirstLogin(true);
        insertUser.setEnabled(true);
        insertUser.setCompanyId(user.getCompanyId());
        insertUser.setMobile(userChangeVo.getUsername());
        insertUser.setFirstname(userChangeVo.getName());
        insertUser.setAuthorities(Lists.newArrayList(Authority.ROLE_USER));
        // 手机号码后6位
        insertUser.setPassword(BCryptPasswordEncoderUtils.encryptPassword(userChangeVo.getUsername().substring(5, 11)));
        insertUser.init();
        userRepository.insert(insertUser);
        return builder.buildSuccessResultInfo("插入成功");
    }

    public static void main(String[] args) {
        String mobile = "18612130100";
        String i = mobile.substring(5, 11);
        System.out.println(i);
    }
}
